package nxu.it.controller;

import nxu.it.model.Todo;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.annotation.Get;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Post;
import org.noear.solon.annotation.Param;
import org.noear.solon.boot.web.MimeType;
import org.noear.solon.core.handle.MethodType;
import org.noear.solon.core.handle.ModelAndView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

@Controller
public class TodoController {
    List<Todo> todoList = new ArrayList<>(Arrays.asList(
            new Todo("吃饭", false),
            new Todo("睡觉", true),
            new Todo("打豆豆", true)
    ));

    @Mapping(value="/todo/list", method = MethodType.GET, produces = MimeType.TEXT_HTML_VALUE)
    public ModelAndView list() {
        ModelAndView mv = new ModelAndView("todo/list.ftl");
        mv.put("todoList", todoList);
        return mv;
    }

    @Mapping(value="/todo/save", method = MethodType.POST, produces = MimeType.TEXT_HTML_VALUE)
    public ModelAndView save(String content) {
        Todo todo = new Todo(content);
        todoList.add(todo);
        return new ModelAndView("todo/success.ftl");
    }

    @Mapping(value = "/todo/check", method = MethodType.GET, produces = MimeType.TEXT_HTML_VALUE)
    public ModelAndView check(@Param("id") int id) {
        Optional<Todo> todoOpt = todoList.stream().filter(todo -> todo.getId() == id).findFirst();
        if (todoOpt.isPresent()) {
            Todo todo = todoOpt.get();
            ModelAndView mv = new ModelAndView("todo/check.ftl");
            mv.put("todo", todo);
            return mv;
        } else {
            return new ModelAndView("todo/error.ftl").put("message", "待办事项未找到");
        }
    }

    @Mapping(value = "/todo/edit", method = MethodType.GET, produces = MimeType.TEXT_HTML_VALUE)
    public ModelAndView edit(@Param("id") int id) {
        Optional<Todo> todoOpt = todoList.stream().filter(todo -> todo.getId() == id).findFirst();
        if (todoOpt.isPresent()) {
            Todo todo = todoOpt.get();
            ModelAndView mv = new ModelAndView("todo/edit.ftl");
            mv.put("todo", todo);
            return mv;
        } else {
            return new ModelAndView("todo/error.ftl").put("message", "待办事项未找到");
        }
    }

    @Mapping(value = "/todo/update", method = MethodType.POST, produces = MimeType.TEXT_HTML_VALUE)
    public ModelAndView update(@Param("id") int id, @Param("content") String content, @Param("completed") boolean completed) {
        Optional<Todo> todoOpt = todoList.stream().filter(todo -> todo.getId() == id).findFirst();
        if (todoOpt.isPresent()) {
            Todo todo = todoOpt.get();
            todo.setContent(content);
            todo.setCompleted(completed);
            return new ModelAndView("todo/success.ftl");
        } else {
            return new ModelAndView("todo/error.ftl").put("message", "待办事项未找到");
        }
    }

    @Mapping(value = "/todo/delete", method = MethodType.GET, produces = MimeType.TEXT_HTML_VALUE)
    public ModelAndView delete(@Param("id") int id) {
        todoList.removeIf(todo -> todo.getId() == id);
        return new ModelAndView("todo/success.ftl");
    }
}